﻿using System.Collections.Generic;
using System.Linq;
using FoodRaidersWebAPI.domainnew.Article;
using FoodRaidersWebAPI.Models;
using FoodRaidersWebAPI.Service.Second;

namespace FoodRaidersWebAPI.Service.First
{
    public class RaiderArticleService
    {
        private readonly FoodContext context;
        private readonly ArticleService articleService;
        private readonly RaiderService raiderService;

        public RaiderArticleService(FoodContext context)
        {
            this.context = context;
            articleService = new ArticleService(context);
            raiderService = new RaiderService(context);
        }
        
        //增加 攻略分享
        public void addRaiderArticle(RaiderArticle raiderArticle)
        {
            context.RaiderArticles.Add(raiderArticle);
            context.SaveChanges();
        }
        
        //删除 攻略分享
        public RaiderArticle delete(int id)
        {
            var raiderArticle = findByID(id);
            context.RaiderArticles.Remove(raiderArticle);
            context.SaveChanges();
            raiderArticle.Article = articleService.delete(raiderArticle.ArticleID);
            raiderArticle.Raider = raiderService.delete(raiderArticle.RaiderID);
            return raiderArticle;
        }
        
        //id查询 对象不加载
        public RaiderArticle findByID(int id)
        {
            return context.RaiderArticles.FirstOrDefault(r => r.RaiderArticleID == id);
        }
        //id查询 不加载评论（用户）
        public RaiderArticle findByIDFullNoCommentLoad(int id)
        {
            var raiderArticle = findByID(id);
            raiderArticle.Article = articleService.findByIDNoCommentLoad(raiderArticle.ArticleID);
            raiderArticle.Raider = raiderService.findByID(raiderArticle.RaiderID);
            return raiderArticle;
        }
        //id查询 全加载
        public RaiderArticle findByIDFullLoad(int id)
        {
            var raiderArticle = findByID(id);
            raiderArticle.Article = articleService.findByIDFullLoad(raiderArticle.ArticleID);
            raiderArticle.Raider = raiderService.findByID(raiderArticle.RaiderID);
            return raiderArticle;
        }
        //id的收藏数+1
        public void addFavorite(int id)
        {
            var raiderArticle = context.RaiderArticles.FirstOrDefault(r => r.RaiderArticleID == id);
            articleService.addFavorite(raiderArticle.ArticleID);
        }
        //id的收藏数-1
        public void subFavorite(int id)
        {
            var raiderArticle = context.RaiderArticles.FirstOrDefault(r => r.RaiderArticleID == id);
            articleService.subFavorite(raiderArticle.ArticleID);
        }
        //id的点赞数+1
        public void addPraise(int id)
        {
            var raiderArticle = context.RaiderArticles.FirstOrDefault(r => r.RaiderArticleID == id);
            articleService.addPraise(raiderArticle.ArticleID);
        }
        //id的点赞数-1
        public void subPraise(int id)
        {
            var raiderArticle = context.RaiderArticles.FirstOrDefault(r => r.RaiderArticleID == id);
            articleService.subPraise(raiderArticle.ArticleID);
        }
        //返回作者id
        public int authorID(int id)
        {
            return findByID(id).UserID;
        }
        //添加评论
        public void addComment(int rid, int uid, string content)
        {
            var raiderArticle= findByID(rid);
            articleService.addComment(raiderArticle.ArticleID,uid,content);
        }
        //返回按点赞数降序的前n位攻略分享 不加载评论
        public List<RaiderArticle> ListByPraise(int n)
        {
            var contextMerchantArticles = context.RaiderArticles.ToList();
            foreach (var merchantArticle in contextMerchantArticles)
            {
                merchantArticle.Article = articleService.findByIDNoCommentLoad(merchantArticle.ArticleID);
            }
            var list=contextMerchantArticles.OrderByDescending(m => m.Article.PraiseNumber).Take(n).ToList();
            list.ForEach(l =>
            {
                l.Raider = raiderService.findByID(l.RaiderID);
            });
            return list;
        }
        //返回按发布时间降序的前n位攻略分享 不加载评论
        public List<RaiderArticle> ListByDateTime(int n)
        {
            var contextMerchantArticles = context.RaiderArticles.ToList();
            foreach (var merchantArticle in contextMerchantArticles)
            {
                merchantArticle.Article = articleService.findByIDNoCommentLoad(merchantArticle.ArticleID);
            }
            var list=contextMerchantArticles.OrderByDescending(m => m.Article.DateTime).Take(n).ToList();
            list.ForEach(l =>
            {
                l.Raider = raiderService.findByID(l.RaiderID);
            });
            return list;
        }
    }
}